package com.srpingde.hhh;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TabHost;
import android.widget.TextView;

import com.nrs.utils.tools.CrashHandler;
import com.springde.hhh.R;
import com.srpingde.hhh.application.HhhApp;

public class MainActivity extends TabActivity {
	private TabHost tabHost;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CrashHandler crashHandler = CrashHandler.getInstance() ;
		crashHandler.init(this);

        writeJSON();
		tabHost=MainActivity.this.getTabHost();
    	tabHost.setup();
    	TabHost.TabSpec spec;
    	Intent intent;
    	
    	RelativeLayout main = (RelativeLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.minitab, null);
    	ImageView imageView = (ImageView) main.findViewById(R.id.tab_iv);
    	RelativeLayout tab_rl = (RelativeLayout) main.findViewById(R.id.tab_rl);
    	imageView.setImageResource(R.drawable.topactivity_home);
        layout(tab_rl);
       // articleTabLabel.setBackgroundResource(R.drawable.main);
         
        RelativeLayout fenlei = (RelativeLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.minitab, null);
        ImageView fenleiimageView = (ImageView) fenlei.findViewById(R.id.tab_iv);
        RelativeLayout fenlei_tab_rl = (RelativeLayout) fenlei.findViewById(R.id.tab_rl);
        fenleiimageView.setBackgroundResource(R.drawable.topactivity_classify);
        layout(fenlei_tab_rl);
       // feedTabLabel.setBackgroundResource(R.drawable.main_fenlei);
         
        RelativeLayout user = (RelativeLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.minitab, null);
        ImageView userimageView = (ImageView) user.findViewById(R.id.tab_iv);
        RelativeLayout user_tab_rl = (RelativeLayout) user.findViewById(R.id.tab_rl);
        userimageView.setBackgroundResource(R.drawable.topactivity_user);
        layout(user_tab_rl);
       // bookTabLabel.setBackgroundResource(R.drawable.main_user);
        
        RelativeLayout weiQuTianDi = (RelativeLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.minitab, null);
        ImageView weiQuTianDiimageView = (ImageView) weiQuTianDi.findViewById(R.id.tab_iv);
        RelativeLayout weiQuTianDi_tab_rl = (RelativeLayout) weiQuTianDi.findViewById(R.id.tab_rl);
        weiQuTianDiimageView.setBackgroundResource(R.drawable.topactivity_hhh);
        layout(weiQuTianDi_tab_rl);
       // setting_tv.setBackgroundResource(R.drawable.main_weiqu);
        
        RelativeLayout more = (RelativeLayout) LayoutInflater.from(MainActivity.this).inflate(R.layout.minitab, null);
        ImageView moreimageView = (ImageView) more.findViewById(R.id.tab_iv);
        RelativeLayout more_tab_rl = (RelativeLayout) more.findViewById(R.id.tab_rl);
        moreimageView.setBackgroundResource(R.drawable.topactivity_more);
        layout(more_tab_rl);
       // more_tv.setBackgroundResource(R.drawable.main_more);
    	
    	intent=new Intent().setClass(MainActivity.this,TopActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    	spec=tabHost.newTabSpec("首页")
    			.setIndicator(main)
    			.setContent(intent);
    	tabHost.addTab(spec);
    	
    	intent=new Intent().setClass(this, FenLeiActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    	spec=tabHost.newTabSpec("分类").setIndicator(fenlei)
    	.setContent(intent);
    	tabHost.addTab(spec);
    	
    	intent=new Intent().setClass(this, UserActivity.class);
    	spec=tabHost.newTabSpec("用户").setIndicator(user)
    			.setContent(intent);
    	tabHost.addTab(spec);
    	
    	intent=new Intent().setClass(this, HHHianDiActivity.class);
    	spec=tabHost.newTabSpec("红花会").setIndicator(weiQuTianDi)
    			.setContent(intent);
    	tabHost.addTab(spec);
    	
    	intent=new Intent().setClass(this, MoreActivity.class);
    	spec=tabHost.newTabSpec("更多").setIndicator(more)
    			.setContent(intent);
    	tabHost.addTab(spec);
    	tabHost.setCurrentTab(0);
    }
    
    private void layout(RelativeLayout textView){
    	RelativeLayout.LayoutParams layoutParams = (LayoutParams) textView.getLayoutParams();
    	layoutParams.height = (int)(HhhApp.getInstance().getDisplayHightAndWightPx()[0] * 0.065);
    	textView.setLayoutParams(layoutParams);
    }
    @Override  
	public boolean dispatchKeyEvent(KeyEvent event) {  
	    if (event.getKeyCode() == KeyEvent.KEYCODE_BACK  
	            && event.getAction() == KeyEvent.ACTION_DOWN  
	            && event.getRepeatCount() == 0) {             
	        //具体的操作代码   
	    	new AlertDialog.Builder(MainActivity.this)
			.setTitle("温馨提示")
			.setIcon(android.R.drawable.ic_menu_help)
			.setMessage("您确定要退出红花会吗？")
			.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					finish();
				}
			})
			.setNegativeButton("取消", null).create().show();
	    }  
	    return super.dispatchKeyEvent(event);  
	}
    public void writeJSON() {
	    try {
	    	JSONArray array  = new JSONArray();
			for(int i = 0;i>4;i++){
				JSONObject object = new JSONObject();
				object.put("name", "Jack Hack");
			    object.put("score", i+"===json数据=");
			    object.put("current", i+"===222222222json数据=");
			}
			Log.e("mytag", array.toString()+"================生成的json==================");
	    } catch (JSONException e) {
	        e.printStackTrace();
	    }
	}
    
}
